﻿using Mozhi.Abp.Core.Offices.Excels;
using Mozhi.Abp.Core.Offices;
using mozhi.smarterp.Finance.Querys;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using mozhi.smarterp.Finance.Basedatas.ThirdParties;
using mozhi.smarterp.Finance.Basedatas;

namespace mozhi.smarterp.Finance.Ieps.Export
{
    public class ThirdpartyExportHandler : ExportHandlerBase<ThirdPartyDto>
    {
        private IThirdPartyAppService _appService;
        private string _moduleName = "第三方";
        public ThirdpartyExportHandler(IServiceProvider serviceProvider) : base(serviceProvider)
        {
            _appService = serviceProvider.GetService(typeof(IThirdPartyAppService)) as IThirdPartyAppService;
        }

        protected override async Task<List<ThirdPartyDto>> GetDatasAsync()
        {
            var filter = _query.Filter.IsNullOrEmpty() ? new PageQuery() { PageIndex = 1, PageSize = int.MaxValue } :
                JsonConvert.DeserializeObject<PageQuery>(_query.Filter);

            var thirdparties = await _appService.FindAsync(filter);

            return thirdparties.Items as List<ThirdPartyDto>;
        }

        protected override async Task<ExcelDocument> GetExcelDocumentAsync()
        {
            var excelDocument = new ExcelDocument();

            excelDocument.FileName = _moduleName;
            excelDocument.FileType = FileType.XLSX;

            excelDocument.Sheets = GetExcelSheets();

            return excelDocument;

        }

        #region

        private List<ExcelSheet> GetExcelSheets()
        {
            var sheet = new ExcelSheet();
            sheet.SheetName = _moduleName;

            sheet.Columns = GetExcelSheetColumns();

            if (_query.IsTemplate || _datas.Count == 0)
                return new List<ExcelSheet>() { sheet };

            sheet.Rows = new List<ExcelSheetRow>();

            foreach (var thirdparty in _datas)
            {
                var row = GetExcelSheetRow(thirdparty);

                sheet.Rows.Add(row);
            }

            return new List<ExcelSheet>() { sheet };
        }

        private List<ExcelSheetColumn> GetExcelSheetColumns()
        {
            var columns = new List<ExcelSheetColumn>
            {
                new ExcelSheetColumn()
                {
                    Caption = "编码",
                    Field = "Code",
                    IsRequired = true,
                },
                new ExcelSheetColumn()
                {
                    Caption = "名称",
                    Field = "Name",
                    IsRequired = true,
                },
                new ExcelSheetColumn()
                {
                    Caption = "往来科目",
                    Field = "CurrentAccountName",
                    IsRequired = true,
                }
            };

            return columns;
        }

        private ExcelSheetRow GetExcelSheetRow(ThirdPartyDto thirdParty)
        {
            var row = new ExcelSheetRow();

            row.Cells =
            [
                new ExcelSheetCell()
                {
                    Value = thirdParty.Code,
                    ValueType = CellValueType.Text
                },
                new ExcelSheetCell()
                {
                    Value = thirdParty.Name,
                    ValueType = CellValueType.Text
                },
                new ExcelSheetCell()
                {
                    Value = thirdParty.CurrentAccountName,
                    ValueType = CellValueType.Text
                },
            ];

            return row;
        }
        #endregion
    }
}
